'Master server protocol, client portion, to be distributed to the nodes.'
'''Comms between multiple nodes and one master server. 

This is the node side of the comms. Server-side handler is in master.handler.
'''
import struct, cPickle, time

def push_request(stats, nodes):
    'Pushes local stats, and neighbour up/down status changes.'
    # serialize data
    tup = (time.time(),stats,nodes)
    serial = cPickle.dumps(tup)
    return '\1'+serial

def push_response(data):
    # gonna have to think about this a lil'...
    # should have a version or something, or node list timestamp
    data = data[1:] # cut off '\1' packet identifier
    return struct.unpack('I', data[:4])[0]

def nodes_request():
    return '\2'

def nodes_response(data):
    if data=='nope':
        raise RuntimeError('This hostname is not on the server''s list!')
    data = data[1:] # cut off '\2' identifier
    return cPickle.loads(data)

